import 'dart:io';

import 'package:flutter_test/flutter_test.dart';

Future<String> lookUpVersion() async => '1.0.0';

Future<String> expensiveWork(String value) async {
  print(value);
  sleep(Duration(seconds: 2));
  return "add1 $value";
}

Future<String> lengthyComputation(String value) async {
  print(value);
  sleep(Duration(seconds: 3));
  return "add2 $value";
}

main() {
  test("test1", () {
    Future<String> result = lookUpVersion();
    print(result.toString());
  });

  test("test2", () {
    //then().catchError() 组合是 try-catch 的异步版本。
    lookUpVersion().then((String result) {
      print(result);
    }).catchError((e, s) {
      // Handle or ignore the error.
    });
  });

  test("test3", () {
    Future result = lookUpVersion();
    result
        .then((value1) => expensiveWork(value1))
        .then((value2) => lengthyComputation(value2))
        .then((value3) => print('Done! $value3'))
        .catchError((exception) {});
  });

  test("test4", () async {
    try {
      final value1 = await lookUpVersion();
      final value2 = await expensiveWork(value1);
      final value3 = await lengthyComputation(value2);
      print('Done! $value3');
    } catch (e) {}
  });

  test("test5", () async {
    List<String> values = await Future.wait<String>([
      lookUpVersion(),
      lookUpVersion(),
      lookUpVersion(),
    ]);
    print('Done with all the long steps!$values');
  });
}
